package com.example.springbootpro.controller.mysql;

import com.example.springbootpro.empty.Referral;
import com.example.springbootpro.exception.BusinessException;
import com.example.springbootpro.result.Result;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/referral")
public class ReferralController {

    @Autowired
    @Qualifier("mysqlJdbcTemplate")
    private JdbcTemplate jdbcTemplate;

    private final RowMapper<Referral> userRowMapper = new BeanPropertyRowMapper<>(Referral.class);
    @PostMapping(value = "save")
    @SneakyThrows
    public Result<String> save(@RequestBody Referral referral)
    {
        System.out.println(referral);
       String  sql = "INSERT INTO medical_record (\n" +
               "    patient_name,\n" +
               "    patient_id,\n" +
               "    disease_diagnosis,\n" +
               "    diagnosis_details,\n" +
               "    contact_number,\n" +
               "    referral_doctor,\n" +
               "    referral_doctor_id,\n" +
               "    referral_signature,\n" +
               "    referral_date,\n" +
               "    project_category,\n" +
               "    treatment_result,\n" +
               "    operator_name, \n" +
               "    notes,\n" +
               "    record_status\n" +
               ") VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        int insertResult = jdbcTemplate.update(sql, referral.getPatient_name(),referral.getPatient_id(),
                referral.getDisease_diagnosis(),referral.getDiagnosis_details(),referral.getContact_number(),referral.getReferral_doctor(),
                referral.getReferral_doctor_id(),referral.getReferral_signature(),referral.getReferral_date(),referral.getProject_category(),
                referral.getTreatment_result(),referral.getOperator_name(),referral.getNotes(),referral.getRecord_status());
        if (insertResult==1)
        {
            return  Result.success("保存数据成功");
        }else {
            throw new BusinessException("保存数据失败");
        }
    }


}
